package hotel.meituan.oversea.biz.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.dto.meituan.MtHotelRoomBedInfoDto;
import hotel.base.oversea.dto.meituan.MtHotelRoomDto;
import hotel.base.oversea.utils.Result;
import hotel.base.oversea.vo.meituan.request.room.MtHotelRealRoomVo;
import hotel.base.oversea.vo.meituan.request.room.MtHotelRoomParamVo;
import hotel.base.oversea.vo.meituan.request.room.MtHotelRoomVo;
import hotel.meituan.oversea.api.vo.up.response.goods.MtHotelGoodsStatusDetailCallbackVo;
import hotel.meituan.oversea.biz.config.service.RedisService;
import hotel.meituan.oversea.biz.service.MtHotelRoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 美团酒店房型Controller
 * @author qiuyy
 * @date 2020-05-15
 */
@Slf4j
@RestController
@RequestMapping("/mtHotelRoom")
@Api(value = "MtHotelRoomController", tags = {"美团酒店房型接口"})
public class MtHotelRoomController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RedisService redisService;
    @Autowired
    private MtHotelRoomService mtHotelRoomService;
    @PostMapping("/getMtHotelRoomById")
    @ApiOperation(value = "根据房型id获取房型信息",notes="必须经过auth授权")
    public Result<MtHotelRoomDto> getMtHotelRoomById(@RequestBody JSONObject jsonObject){
        String roomId = jsonObject.getString("roomId");
        String mtHotelId = jsonObject.getString("mtHotelId");
        if(StringUtils.isEmpty(roomId)){
            return new Result<>("roomId 不可为空",CommonConstants.REQUEST_ERROR);
        }
        if(StringUtils.isEmpty(mtHotelId)){
            return new Result<>("mtHotelId 不可为空",CommonConstants.REQUEST_ERROR);
        }
        return mtHotelRoomService.getMtHotelRoomById(mtHotelId,roomId);
    }
    @PostMapping("/getMtHotelRoomBedInfoById")
    @ApiOperation(value = "根据房型id获取床型信息",notes="必须经过auth授权")
    public Result<List<MtHotelRoomBedInfoDto>> getMtHotelRoomBedInfoById(@RequestBody JSONObject jsonObject){
        String roomId = jsonObject.getString("roomId");
        String mtHotelId = jsonObject.getString("mtHotelId");
        if(StringUtils.isEmpty(roomId)){
            return new Result<>("roomId 不可为空",CommonConstants.REQUEST_ERROR);
        }
        if(StringUtils.isEmpty(mtHotelId)){
            return new Result<>("mtHotelId 不可为空",CommonConstants.REQUEST_ERROR);
        }
        return mtHotelRoomService.getMtHotelRoomBedInfoById(mtHotelId,roomId);
    }
    @PostMapping("/getMtHotelRoom")
    @ApiOperation(value = "获取物理房型接口",notes="必须经过auth授权")
    public Result<Object> getMtHotelRoom(@RequestBody MtHotelRoomParamVo mtHotelRoomParamVo){
        //参数校验
        Map<String,String> error = new HashMap();
        List<Long> hotelIds = mtHotelRoomParamVo.getHotelIds();
        if(null == hotelIds){
            error.put("code","1000");
            error.put("message","hotelIds不可为空");
            return new Result<>("hotelIds不可为空",CommonConstants.REQUEST_ERROR);
        }
        return mtHotelRoomService.getMtHotelRoom(hotelIds);
    }
    @PostMapping("/getMtHotelRoomForSw")
    @ApiOperation(value = "获取物理房型根据美团酒店id",notes="必须经过auth授权")
    public Result<Object> getMtHotelRoomForSw(@RequestBody Map map){
        Map<String,String> error = new HashMap();
        if(null == map.get("mtHotelId")){
            error.put("code","1000");
            error.put("message","mtHotelId不可为空");
            return new Result<>("mtHotelId不可为空",CommonConstants.REQUEST_ERROR);
        }
        String mtHotelId = (String)map.get("mtHotelId");
        log.info("===========请求的美团酒店id为{}=====",mtHotelId);
        //发起请求并保存数据
        return mtHotelRoomService.getMtHotelRoomByHotelId(mtHotelId);
    }
    @PostMapping("/saveRoomData")
    @ApiOperation(value = "保存缓存的酒店物理房型接口",notes="必须经过auth授权")
    public Result<Object> saveHotelInfos(@RequestBody MtHotelRoomVo mtHotelRoomVo){
        //发起请求并保存数据
        Map<Long, List<MtHotelRealRoomVo>> infos = mtHotelRoomVo.getRealRoomInfos();
        if(null == infos || infos.size()==0){
            return new Result<>("参数mtHotelRoomVo不可为空", CommonConstants.REQUEST_ERROR);
        }
        return mtHotelRoomService.saveRoomData(infos);
    }

    /**
     * 用途：处理房态异步通知定时器调用
     * @param map
     * @return
     */
    @PostMapping("/dealNotifyRoomFromJob")
    @ApiOperation(value = "处理房态变更通知",notes="必须经过auth授权")
    public Result<Object> aspDealNotifyRoomFromJob(@RequestBody Map map){
        log.info("  处理房态变更通知  dealNotifyRoomFromJob:{}", JSON.toJSONString(map));
        //发起请求并保存数据
        String str = (String)map.get("goods");
        if(null == str){
            return new Result<>("goods 不可为空", CommonConstants.REQUEST_ERROR);
        }
        List<MtHotelGoodsStatusDetailCallbackVo> infos = JSON.parseArray(str, MtHotelGoodsStatusDetailCallbackVo.class);//一个酒店多个产品状态信息
        return mtHotelRoomService.dealNotifyRoomFromJob(infos);
    }

}
